//
//  ViewController.m
//  youmengdemo
//
//  Created by 张乐乐 on 2020/6/28.
//  Copyright © 2020 taohuikeji. All rights reserved.
//

#import "ViewController.h"
#import <UMCommon/UMCommon.h>
#import <UMShare/UMShare.h>
#import <UMCommonLog/UMCommonLogHeaders.h>
#import "WXApi.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"授权" forState:0];
    [button setTitleColor:[UIColor whiteColor] forState:0];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10;
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(200, 200, 100, 100);
    [button1 setTitle:@"分享" forState:0];
    [button1 setTitleColor:[UIColor whiteColor] forState:0];
    button1.layer.masksToBounds = YES;
    button1.layer.cornerRadius = 10;
    [button1 setBackgroundColor:[UIColor redColor]];
    [button1 addTarget:self action:@selector(shareClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}

-(void)buttonClick{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:self completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"error------>>>>>>>%@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            NSLog(@"Wechat openid: %@---->>>%@", resp.openid,resp.accessToken);
            NSDictionary *parameters = @{@"weixinOpenId":resp.openid,
                                         @"WxUnionId":resp.unionId,
                                         @"WxAccessToken":resp.accessToken,};
            NSLog(@"parameters------>>>>>>%@",parameters);
        }
    }];
}


-(void)shareClick{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    [shareObject setShareImage:[@"https://img.alicdn.com/i1/2206692357221/O1CN018u7mgO23DGaa5o8aY_!!2206692357221.jpg" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            
        }else{
            
        }
    }];
}

@end
